package com.neusoft.neuiotms.dcom.service.incident.impl;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.incident.IncidentHandleConverter;
import com.neusoft.neuiotms.dcom.dto.incident.IncidentHandleDTO;
import com.neusoft.neuiotms.dcom.model.enums.IncidentHandleStatus;
import com.neusoft.neuiotms.dcom.model.incident.IncidentHandle;
import com.neusoft.neuiotms.dcom.repository.incident.IncidentHandleAttachmentRepository;
import com.neusoft.neuiotms.dcom.repository.incident.IncidentHandleRepository;
import com.neusoft.neuiotms.dcom.service.incident.IncidentHandleService;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:06:12
 */
@Service
public class IncidentHandleServiceImpl implements IncidentHandleService {
    @Autowired
    private IncidentHandleRepository incidentHandleRepository;
    @Autowired
    private IncidentHandleConverter incidentHandleConverter;
    @Autowired
    private FileService fileService;
    @Autowired
    private IncidentHandleAttachmentRepository incidentHandleAttachmentRepository;
    @Autowired
    private ProcessInstanceService processInstanceService;

    @Override
    @Transactional
    public IncidentHandleDTO create(final IncidentHandleDTO dto, final UserBean user) {
        // 查询当前workNum和TaskId下是否存在记录
        final IncidentHandle oldModel = this.incidentHandleRepository.findByWorkNumAndTaskId(
                dto.getWorkOrderNum(), dto.getTaskId());
        if (oldModel == null) { // 如果不存在新增
            dto.setId(null);
        }

        final IncidentHandle model = this.incidentHandleConverter.toModel(dto);
        this.incidentHandleRepository.save(model);

        if (!IncidentHandleStatus.temporary.name().equals(dto.getStatus().name())) {
            // 完成节点
            this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);
        }

        return this.incidentHandleConverter.toDTO(model);
    }

    @Override
    public IncidentHandleDTO detail(final String workOrderNum, final String taskId,
            final UserBean user) {
        IncidentHandle model = null;
        try {
            if (StringUtils.isNotBlank(workOrderNum) && !"NULL".equals(taskId)) {
                model = this.incidentHandleRepository.findByWorkNumAndTaskId(workOrderNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String fromProcessTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (fromProcessTaskId == null) {
                        return new IncidentHandleDTO();
                    } else {
                        model = this.incidentHandleRepository.findByWorkNumAndTaskId(workOrderNum,
                                fromProcessTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.incidentHandleRepository.findByWorkNum(workOrderNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workOrderNum },
                    new Object[] {taskId });
        }

        return this.incidentHandleConverter.toDTO(model);
    }

    @Override
    @Transactional
    public void deleteMongoFile(final String fsId) {
        this.fileService.deleteByFsId(fsId);
        this.incidentHandleAttachmentRepository.deleteByFsId(fsId);
    }

    @Override
    public IncidentHandleDTO findByWorkNumAndTaskId(final String workNum, final String taskId) {
        final IncidentHandle model = this.incidentHandleRepository.findByWorkNumAndTaskId(workNum, taskId);
        return this.incidentHandleConverter.toDTO(model);
    }

    @Override
    public void deleteByWorkNum(final String workNum) {
        this.incidentHandleRepository.deleteByWorkNum(workNum);
    }

    @Override
    public IncidentHandleDTO findByWorkNum(final String workNum) {
        final IncidentHandle model = this.incidentHandleRepository.findByWorkNum(workNum);
        if (model != null) {
            return this.incidentHandleConverter.toDTO(model);
        } else {
            return null;
        }
    }
}
